package com.chaosj.memory.handlers;

import com.chaosj.webmagic.IScrapeHandler;
import com.chaosj.webmagic.dto.SearchRequestDto;
import com.chaosj.webmagic.scrape.SearchScrapeHandler;

import java.util.ServiceLoader;

/**
 * @className: GuShiWenHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/11
 **/
public class GuShiWenHandler extends AbstractToolHandler<SearchRequestDto>{

    @Override
    public String name() {
        return "gsw";
    }

    @Override
    public void handler(SearchRequestDto req, StringBuffer outBuffer) {
        ServiceLoader.load(IScrapeHandler.class).forEach(handler->{
            if( handler instanceof SearchScrapeHandler){
                handler.scrape(req);
                outBuffer.append(handler.getResult(req));
            }
        });
        outBuffer.append("*****************************************************************\n                        下一个handler                        \n*****************************************************************\n");
    }

    @Override
    public SearchRequestDto cmd2Dto(String[] args) {
        int type = Integer.valueOf(args.length>1? args[1] : "0");
        String cmd = args.length> 2 ? args[2] : null;
        String search = args.length > 3 ? args[3] : null;
        String page = args.length > 4 ? args[4] : null;
        return new SearchRequestDto().setType(type).setSearch(search).setCmd(cmd).setPage(page!=null?Integer.valueOf(page):null);
    }
}
